package persistent.prestige.console.netty.dubbo.net;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import persistent.prestige.console.netty.dubbo.utils.LogUtils;

/**
 * 响应信息 编码器，，用于服务器
 * @author dingwei2
 * 
 * 响应报文格式
 * 4字节的请求序列号 2字节的执行状态码 2字节数据长度 多字节的响应数据信息(data)
 *
 */
public class ResponseFrameEncoder extends MessageToByteEncoder<Response> {

	@Override
	protected void encode(ChannelHandlerContext ctx, Response msg, ByteBuf out) throws Exception {
		// TODO Auto-generated method stub
		LogUtils.log("ResponseFrameEncoder:服务端编码响应,requestId:%d,code:%d,data:%s", msg.getRequestId(), msg.getCode(), msg.getData());
		out.writeInt(msg.getRequestId());
		out.writeShort(msg.getCode());
		byte[] data = msg.getData().getBytes();
		out.writeShort(data.length);
		out.writeBytes(data);
		LogUtils.log("ResponseFrameEncoder:服务端编码响应成功");

	}
	
	

}
